<template>
  <div class="areamanagement-page">
    <!-- 头部搜索区域 -->
    <el-card class="search" shadow="never">
      <el-form :inline="true" :model="formSearch" class="form-inline">
        <el-form-item label="区域搜索 :">
          <el-input
            v-model="formSearch.name"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onQuery"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 头部搜索区域 -->
    <!-- cardbody -->
    <!-- 新建区域 -->
    <el-card>
      <el-row class="operation">
        <el-button
          style="margin-bottom: 10px"
          type="warning"
          class="el-button--second"
          icon="el-icon-circle-plus-outline"
          @click="OpenDialog('add')"
          >新建</el-button
        >
      </el-row>
      <!-- 新建区域  -->

      <!-- 页面表格 -->
      <el-table
        :data="tableData"
        highlight-current-row
        style="width: 100%"
        :row-style="{ hight: '44px' }"
        :cell-style="{ padding: '0px' }"
        v-loading="loading"
      >
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="区域名称" width="240">
        </el-table-column>
        <el-table-column prop="nodeCount" label="点位数" width="280">
        </el-table-column>
        <el-table-column prop="remark" label="备注说明" width="280" />
        <el-table-column label="操作" width="240">
          <template v-slot="{ row }">
            <el-button type="text" size="small" @click="clickDetailFn(row.id)"
              >查看详情</el-button
            >
            <el-button type="text" @click="OpenDialog('change', row.id)"
              >修改</el-button
            >
            <el-button type="text" style="color: red" @click="delFn(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格 -->
      <!-- 分页 -->
      <el-pagination
        v-if="page.totalCount > 1"
        prev-text="上一页"
        next-text="下一页"
        @current-change="handleCurrentChange"
        :current-page.sync="page.pageIndex"
        :page-size="page.pageSize"
        layout="slot,prev, next"
      >
        <template>
          <span class="el-pagination__total"
            >共{{ page.totalCount }}条记录&nbsp;&nbsp;第&nbsp;{{
              page.pageIndex
            }}/{{ page.totalPage }}页</span
          >
        </template>
      </el-pagination>
      <!-- 分页 -->
    </el-card>
    <!-- 新建和修改的弹窗  AddOrChangevisible -->
    <el-dialog
      :title="areatitle"
      :visible.sync="AddOrChangevisible"
      width="35%"
      @close="closeFn"
    >
      <el-form
        label-width="120px"
        :model="formData"
        :rules="rules"
        validate
        ref="formData"
      >
        <el-form-item label="区域名称">
          <el-input
            type="text"
            placeholder="请输入内容"
            v-model="formData.regionName"
            maxlength="15"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="备注说明" prop="name">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="formData.name"
            maxlength="40"
            show-word-limit
            :rows="3"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn" style="background-color: #fbf4f0"
          >取 消</el-button
        >
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新建和修改的弹窗  AddOrChangevisible -->

    <!-- 区域详情的弹窗 -->
    <el-dialog title="区域详情" :visible.sync="detailFormVisible">
      <el-form  label-width="140px">
        <el-form-item label="区域名称" prop="name">
          <span>{{ areaname }}</span>
        </el-form-item>
        <el-form-item label="包含点位">
          <el-table :data="detailtableData" style="width: 100%">
            <el-table-column prop="index" label="序号" width="180">
            </el-table-column>
            <el-table-column prop="name" label="点位名称" width="180">
            </el-table-column>
            <el-table-column prop="vmCount" label="设备数量"> </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 详情的弹窗 -->
    <!-- cardbody  changeRegion, -->
  </div>
</template>

<script>
import {
  getRegionSearchList,
  changeRegion,
  getRegionFn,
  postRegion,
  delRegion,
  getNode
} from '@/api/area'
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      areaname: '',
      page: {
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0,
        totalPage: 0
      },
      formSearch: {
        name: ''
      },
      formData: {
        name: '',
        remark: ''
      },
      AddOrChangevisible: false,
      OpenType: '',
      rules: {
        regionName: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        remark: [{ required: true, message: '请输入备注', trigger: 'blur' }]
      },
      detailtableData: [],
      detailFormVisible: false
    }
  },
  created () {
    this.getRegionSearchList()
  },
  methods: {
    async getRegionSearchList () {
      this.loading = true
      const res = await getRegionSearchList({
        ...this.formSearch,
        ...this.page
      })
      console.log(res)
      this.tableData = res.currentPageRecords
      this.page.totalCount = res.totalCount
      this.page.totalPage = res.totalPage
      this.loading = false
    },
    // 详情   待做
    //  区域详情
    async clickDetailFn (row) {
      this.detailFormVisible = true
      this.areaname = row.name
    },
    // 详情   待做
    handleCurrentChange () {
      this.getRegionSearchList()
    },
    onQuery () {
      this.getRegionSearchList()
    },
    // 弹窗
    closeFn () {
      this.AddOrChangevisible = false
      this.$refs.formData.resetFields()
    },
    async OpenDialog (type, id) {
      this.AddOrChangevisible = true
      // / 值不同。点击显示的不同
      this.OpenType = type
      if (type !== 'add') {
        const res = await getRegionFn(id)
        // 回显  把data里面的数据给到form
        this.formData = res
      }
    },
    async submit () {
      // 提交时判断 是新增内容 还是 编辑内容 接口不同
      // 先检查 表单是否符合效验规则
      await this.$refs.formData.validate()
      if (this.OpenType === 'add') {
        // 如果是新增 add 就是 postRegion 这个接口
        const res = await postRegion({ ...this.formData })
        console.log(res)
        this.$message.success('添加成功')
      } else {
        // 、如果并不是新增 那么就是编辑  调用编辑接口
        // 把 id stem  content 解构
        // 修改逻辑不成功  需要改？？？？？？？
        // const { } = this.formData
        this.formData = await changeRegion()// 请求不对
        // console.log()
        this.$message.success('修改成功')
      }
      // 然后再去请求数据  有问题待解决？？？？？
      this.page.pageIndex = 1
      this.closeFn()
      this.getRegionSearchList()
    },
    async delFn (id) {
      await delRegion(id)
      this.getRegionSearchList()
      this.$message.success('删除成功')
    }
  },
  // 计算属性改标题名字
  computed: {
    areatitle () {
      let areatitle = '新增区域'
      if (this.OpenType === 'add') {
        areatitle = '新增区域'
      } else if (this.OpenType === 'change') {
        areatitle = '修改区域'
      }
      return areatitle
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-card search is-never-shadow {
  margin-bottom: 10px;
}
::v-deep .el-card__body {
  padding: 15px;
  margin-top: 5px;
  margin-bottom: -20px;
}
.el-pagination {
  display: flex;
  height: 96px;
  align-items: center;
  .el-pagination__total {
    flex: 1;
    font-size: 16px !important;
    color: #dbdfe5 !important;
  }
  ::v-deep button:disabled {
    color: #d8dde3;
    background: #edf0f9;
  }
  ::v-deep button {
    width: 70px;
    height: 32px;
    margin: 0 16px;
    border-radius: 2px;
    background-color: #d5ddf8;
  }
  .el-table {
  ::v-deep th {
    padding: 10px, 0;
    background-color: rgb(243, 246, 251);
    border: none;
  }
  ::v-deep tr {
    color: #666;
    font-size: 14px;
    padding: 2px 0;
    border: none;
  }
  ::v-deep td {
    padding: 2px 0;
    border: none;
  }
}
}
</style>
